package com.rqf.ls.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.rqf.ls.service.IRegisterOfLawyerService;
import com.rqf.ls.service.IRemindService;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

@Service("RemindService")
public class RemindService implements IRemindService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private IRegisterOfLawyerService lawyerService;
	
	 /**
	 * 用户未读回复条数
	 * @param phoneNo
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月28日
	 */
	@Override
	public Integer userRemind(String phoneNo){
		String sql = "select count(id) from anwser where isable=1 and read=0 and whoque='%s'";
		int queryForInt = jdbcTemplate.queryForInt(String.format(sql, phoneNo));
		return queryForInt;
	}
	 /**
	 * 被提问题条数
	 * @param phoneNo
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月28日
	 */
	@Override
	public Integer lawyerRemind(String phoneNo){
		String sql = "select count(id) from question where isable=1 and read=0 and askwhoid='%s'";
		int queryForInt = jdbcTemplate.queryForInt(String.format(sql, phoneNo));
		return queryForInt;
	}
	
	 /**
	 * 律师已读问题更新状态
	 * @param lawyerPhone
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月29日
	 */
	@Override
	public Boolean setQuestionRead(String lawyerPhone){
		Boolean isSuccess = true;
		String sql = "update from question set read=1 where askwhoid='%s'";
		try {
			jdbcTemplate.execute(String.format(sql, lawyerPhone));
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	
	/**
	 * 已读回复更新状态
	 * @param id
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月29日
	 */
	@Override
	public Boolean setAnswerRead(Integer id){
		Boolean isSuccess = true;
		String sql = "update from anwser set read=1 where questionid='%s'";
		try {
			jdbcTemplate.execute(String.format(sql, id));
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	
	 /**
	 * 发送通知信息
	 * @param phoneNo
	 * @param msg
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月30日
	 */
	@Override
	public Boolean sendMsg(String phoneNo, String msg){
		Boolean isSueccess = true;
		Object nameObj = lawyerService.read(phoneNo, "username");
		String name = "律师";
		if (nameObj != null) {
			name = nameObj.toString() + name;
		}
		//TODO 调用第三方服务发送短信
		String url = "http://gw.api.taobao.com/router/rest";
		String appkey = "23330765";
		String secret = "77de197e7455a3cfc50deb3e7e3a5ee8";
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend("123456");
		req.setSmsType("normal");
		req.setSmsFreeSignName("活动验证");
		req.setSmsParamString("{\"name\":\""+name+"\"}");
		req.setRecNum(phoneNo);
		req.setSmsTemplateCode("SMS_8956423");
		AlibabaAliqinFcSmsNumSendResponse rsp;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			isSueccess = false;
			e.printStackTrace();
		}
		
		return isSueccess;
	}
	
}
